package com.cicadalane.androlate;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

class LanguagePage extends AndrolateWizardPage {
	private static final String androidLanguages[] = new String[] { "ar", "bg",
			"ca", "cs", "da", "de", "el", "en", "es", "es-rUS", "fi-rFI", "fr",
			"hi-rIN", "hr-rHR", "hu-rHU", "id-rID", "it", "ja", "ko", "lt-rLT",
			"lv-rLV", "nb-rNO", "nl", "pl", "pt", "pt-rPT", "ro-rRO", "ru",
			"sk-rSK", "sl-rSI", "sr-rRS", "sv", "th-rTH", "tl-rPH", "tr",
			"uk-rUA", "vi-rVN", "zh-rCN", "zh-rTW" };

	private Button mAndroidLanguagesButton = null;

	private Table mLanguagesTable = null;

	protected LanguagePage(String pageName) {
		super(pageName);
		setTitle(pageName);
		setDescription("Select the target languages");
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);

		GridLayout gl = new GridLayout();
		composite.setLayoutData(new GridData(GridData.FILL));
		composite.setLayout(gl);

		Label instructionLabel = new Label(composite, 0);
		instructionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) instructionLabel.getLayoutData()).horizontalSpan = 2;
		instructionLabel
				.setText("Check the languages you wish to target from the list below.");

		mAndroidLanguagesButton = new Button(composite, SWT.CHECK | SWT.BOLD);
		mAndroidLanguagesButton.setLayoutData(new GridData(
				GridData.FILL_HORIZONTAL));
		((GridData) mAndroidLanguagesButton.getLayoutData()).horizontalSpan = 2;
		mAndroidLanguagesButton.setText("Select Android SDK languages");

		mLanguagesTable = new Table(composite, SWT.MULTI | SWT.V_SCROLL
				| SWT.H_SCROLL | SWT.CHECK);

		mLanguagesTable.setLayoutData(new GridData(GridData.FILL_BOTH));
		mLanguagesTable.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail != SWT.CHECK) {
					return;
				}
				if (!(event.item instanceof TableItem)) {
					return;
				}
				TableItem tableItem = (TableItem) event.item;
				if (tableItem.getData() == null
						|| !(tableItem.getData() instanceof Language))
					return;

				Language language = (Language) tableItem.getData();

				AndrolateLanguage l = new AndrolateLanguage(language
						.getDisplay(), language.getISO639_1(), language
						.getDirectory());
				if (tableItem.getChecked())
					getAndrolateApplication().updateTargetLanguage(l);
				else
					getAndrolateApplication().removeTargetLanguage(l);
				setPageComplete(validatePage());
			}

		});

		for (Language l : GoogleTranslate.Languages) {
			TableItem ti = new TableItem(mLanguagesTable, SWT.NONE);
			ti.setText(new String[] { l.getDisplay() });
			ti.setData(l);
		}

		mAndroidLanguagesButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				ArrayList<String> androidLanguageList = new ArrayList<String>();
				for (int i = 0; i < androidLanguages.length; i++)
					androidLanguageList.add(androidLanguages[i]);

				TableItem tableItems[] = mLanguagesTable.getItems();
				for (int i = 0; i < tableItems.length; i++) {
					TableItem tableItem = tableItems[i];
					if (tableItem.getData() == null
							|| !(tableItem.getData() instanceof Language))
						return;

					Language l = (Language) tableItem.getData();
					if (androidLanguageList.contains(l.getDirectory())) {
						tableItem.setChecked(mAndroidLanguagesButton
								.getSelection());
					}
					if (tableItem.getChecked())
						getAndrolateApplication().updateTargetLanguage(l);
					else
						getAndrolateApplication().removeTargetLanguage(l);
				}
				setPageComplete(validatePage());
			}
		});
		setControl(composite);
	}

	@Override
	public void onEnterPage() {
		super.onEnterPage();
		ArrayList<AndrolateLanguage> targetLanguages = getAndrolate()
				.getTargetLanguages();
		TableItem tableItems[] = mLanguagesTable.getItems();
		for (int i = 0; i < tableItems.length; i++) {
			TableItem ti = tableItems[i];
			Language language = (Language) ti.getData();
			for (AndrolateLanguage al : targetLanguages) {
				if (al.getDirectory().equals(language.getDirectory())) {
					ti.setChecked(true);
					break;
				}
			}
		}
	}

	private boolean validatePage() {
		TableItem tableItems[] = mLanguagesTable.getItems();
		for (int i = 0; i < tableItems.length; i++) {
			TableItem tableItem = tableItems[i];
			if (tableItem.getData() == null
					|| !(tableItem.getData() instanceof Language))
				return false;

			if (tableItem.getChecked()) {
				return true;
			}
		}
		return false;
	}
}
